from django.core.urlresolvers import reverse
from django import template
import string

register = template.Library()

@register.inclusion_tag("polls/tag_sorter.html", takes_context=True)
def sorter(context, field_name='', link_name=''):
    sort_field = context["sort_field"]
    ascending = True
    current = False
    if sort_field and sort_field.find(field_name) >= 0:
        current = True
        if sort_field[0] == '-':
            # do nothing, already ascending
            pass
        else:
            ascending = False
            field_name = '-'+field_name
    url = reverse(context["base_url"], kwargs={'order_by': field_name})
    return {
        "current": current,
        "link_url": url,
        "ascending": ascending,
        "link_name": link_name,
        "MEDIA_URL": context["MEDIA_URL"],
    }
